/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upf.grupoe.p2ppicture.Image.Implementations;

import edu.upf.grupoe.p2ppicture.Image.Interfaces.Visualizer;
import java.awt.Desktop;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * This class represents a slideshow builder and appends the presention to the system default browser
 * @author marc
 */
public class VisualizerImpl implements Visualizer{

    private List<String> pictures = new ArrayList<String>();
    private String filePath = "";

    private static Log logger = LogFactory.getLog("Main");

    /**
     * It creates a SlideShow
     * @param pictures
     */
    public void createSlideShow(List<String> pictures){
         try{
             FileWriter file = new FileWriter(filePath);
             BufferedWriter out = new BufferedWriter(file);
                out.write("<html>");out.write("\n");
                out.write("<!DOCTYPE HTML>");out.write("\n");
                out.write("<head>");out.write("\n");
                out.write("	<title>P2PPicture Slideshow Visualizer</title>");out.write("\n");
                out.write("	<script type=\"text/javascript\">");out.write("\n");
                out.write("		function getCanvasContext(x){");out.write("\n");
                out.write("		   var canvas = document.getElementById(x);");out.write("\n");
                out.write("		   if (canvas.getContext) ");out.write("\n");
                out.write("		   {");out.write("\n");
                out.write("		     var area = canvas.getContext(\"2d\");   ");out.write("\n");
                out.write("		     return area;");out.write("\n");
                out.write("		   }");out.write("\n");
                out.write("		   else");out.write("\n");
                out.write("		     return false;");out.write("\n");
                out.write("		}");out.write("\n");
                out.write("		var i = 0;");out.write("\n");
                out.write("		function draw(){");out.write("\n");
                out.write("			var area = getCanvasContext(\"canvas\");");out.write("\n");
                // Organize Pictures
                out.write("                     var pictures=new Array("+pictures.size()+");"); out.write("\n");
                // Write pictures
                int i = 0;
                Iterator itr = pictures.iterator();
                while(itr.hasNext()){
                    out.write("                 pictures["+i+"] = "+"\""+itr.next()+"\";");
                    out.write("                 \n");
                    i++;
                }
                out.write("			");out.write("\n");
                out.write("			if(area){			");out.write("\n");
                out.write("				area.fillStyle=\"rgb(0,127,255)\";");out.write("\n");
                out.write("				area.fillRect(0,0,screen.width,screen.height);");out.write("\n");
                out.write("				area.strokeStyle = \"rgb(255,255,255)\";");out.write("\n");
                out.write("				area.lineWidth=4;");out.write("\n");
                out.write("				area.beginPath();");out.write("\n");
                out.write("				area.moveTo(0,30);");out.write("\n");
                out.write("				area.lineTo(960,30);");out.write("\n");
                out.write("				area.stroke();");out.write("\n");
                out.write("				img = new Image();	");out.write("\n");
                out.write("				img.onload = function(){");out.write("\n");
                out.write("				    	area.shadowOffsetX=-10;");out.write("\n");
                out.write("				    	area.shadowOffsetY=5;");out.write("\n");
                out.write("				    	area.shadowBlur=10;");out.write("\n");
                out.write("				   	area.shadowColor=\"rgba(0, 0, 0, 0.5)\";					");out.write("\n");
                out.write("				}");out.write("\n");
                out.write("				if(i=="+pictures.size()+"){");out.write("\n");
                out.write("					i=0;");out.write("\n");
                out.write("				}");out.write("\n");
                out.write("				area.save();");out.write("\n");
                out.write("				img.src = pictures[i];				");out.write("\n");
                out.write("				var anchorX; var anchorY; ");out.write("\n");
                out.write("				area.font=\"bold 14px Arial\";");out.write("\n");
                out.write("				if(img.width > 300){");out.write("\n");
                out.write("					var proportion = img.width/300;");out.write("\n");
                out.write("					var outY = (img.height/proportion);");out.write("\n");
                out.write("					anchorX = 480-(300/2);");out.write("\n");
                out.write("					anchorY = 290-(outY/2);");out.write("\n");
                out.write("					area.fillStyle=\"rgb(255,255,255)\";");out.write("\n");
                out.write("					area.fillText(pictures[i],0,20);");out.write("\n");
                out.write("					area.translate(anchorX,anchorY);");out.write("\n");
                out.write("					area.drawImage(img,0,0,300,outY);");out.write("\n");
                out.write("				}else{");out.write("\n");
                out.write("					anchorX = 480-(img.width/2);");out.write("\n");
                out.write("					anchorY = 290-(img.height/2);");out.write("\n");
                out.write("					area.fillStyle=\"rgb(255,255,255)\";");out.write("\n");
                out.write("					area.fillText(pictures[i],0,20);");out.write("\n");
                out.write("					area.translate(anchorX,anchorY);");out.write("\n");
                out.write("					area.drawImage(img,0,0);");out.write("\n");
                out.write("				}");out.write("\n");
                out.write("			}");out.write("\n");
                out.write("			i++;");out.write("\n");
                out.write("			area.restore();");out.write("\n");
                out.write("		}");out.write("\n");
                out.write("		function init(){");out.write("\n");
                out.write("				setInterval(draw,3000);");out.write("\n");
                out.write("		}");out.write("\n");
                out.write("	</script>");out.write("\n");
                out.write("</head>");out.write("\n");
                out.write("<body onLoad=\"init()\">");out.write("\n");
                out.write("	<h1>P2PPicture Slideshow</h1>");out.write("\n");
                out.write("	<canvas id=\"canvas\" width=\"960\" height=\"580\">Your browser can't support HTML5!</canvas>");out.write("\n");
                out.write("</body>");out.write("\n");
                out.write("</html>");out.write("\n");
             out.close();
         }
         catch(IOException ioe){
             logger.error(ioe.getMessage());
         }
    }
    /**
     * This function invokes the system default browser with the presentation
     * @throws IOException
     * @throws URISyntaxException
     */
    public void viewSlideShow() throws IOException, URISyntaxException{
        File htmlFile = new File(filePath);
        htmlFile.deleteOnExit();
        // open the default web browser for the HTML page
        Desktop.getDesktop().browse(htmlFile.toURI());
    }
    /**
     * Visualizer default constructor
     * @param pictures
     * @param filePath
     * @throws URISyntaxException
     * @throws IOException
     */
    public VisualizerImpl(List<String> pictures, String filePath) throws URISyntaxException, IOException{
        this.pictures = pictures;
        this.filePath = filePath;
        createSlideShow(this.pictures);
        viewSlideShow();
    }
}
